const express = require('express');
const router = express.Router();
const { createComment, getComments, updateComment, updateLikeNum } = require('../controller/comments');
const {SuccessModel, ErrorModel} = require('../model/resModel');
const loginCheck = require('../middleWare/loginCheck');
router.get('/getComments', (req, res, next) => {
  const blogId = req.query.blogId;
  console.log('getCOmments-data', blogId);
  const result = getComments(blogId);
  result.then(comments => {
    res.json(comments);
  }).catch(err => {
    res.json(new ErrorModel('获取评论失败'));
  })
});

router.post('/createFstLvComment', loginCheck, (req, res, next) => {
  const data = req.body;
  const result = createComment(data);
  result.then(data => {
    res.json(new SuccessModel("成功创建评论"));
  }).catch(err => {
    res.json(new ErrorModel(err));
  })
});

router.post('/updateCommentReply', loginCheck, (req, res, next) => {
  const data = req.body;
  const result = updateComment(data);
  result.then(msg => {
    res.json(new SuccessModel(msg));
  }).catch(err => {
    res.json(new ErrorModel(err));
  })
});

router.post('/updateLikeNum', (req, res, next) => {
  const item = req.body;
  const result = updateLikeNum(item);
  result.then(msg => {
    res.json(new SuccessModel(msg));
  }).catch(err => {
    res.json(new ErrorModel(err));
  })
});
module.exports = router;